package com.example.myproject;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileServiceFactory;

@SuppressWarnings("serial")
public class ImageShowServlet extends HttpServlet {
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		//file id of thumbnail file 
		Long parentid = Long.parseLong(req.getParameter("parentid"));

		//place where image is stored
		final String FILESYSTEM = "/blobstore/";		
		
		UploadDAO dao = new UploadDAO();
		UploadDTO dto = null;
		
		
		dto =  dao.findById(parentid);

		AppEngineFile AFE = new AppEngineFile(FILESYSTEM + dto.getPath());
		BlobKey bk = FileServiceFactory.getFileService().getBlobKey(AFE);
				
		resp.sendRedirect("/serve?blob-key="+bk.getKeyString());
		
	}
	

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}